package com.ssl.ware.repo;

import com.ssl.ware.pojo.Warehouse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface WarehouseRepo extends JpaRepository<Warehouse, Long> {
    List<Warehouse> getByShopId(Integer shopId);

    @Query(value = "select * from ware_info where shopId=?1 and skuId = ?2 and product_number >= ?3", nativeQuery = true)
    List<Warehouse> getByShopIdAndSkuIdAndLockCount(Integer shopId, Long skuId, Integer lockCount);

    @Query(value = "update ware_info set product_number = product_number - ?1 where product_number >= ?1 and skuId =?2 and spuId =?3 and id =?4", nativeQuery = true)
    @Modifying
    @Transactional
    Integer lockWare(Integer lockCount, Long skuId, Long spuId, Integer wareId);
    // 如果库存锁定成功了，但是用户取消了订单，你如何回滚库存?
    // 如果库存锁定成功了，过了15mins，用户不买了，你如何回滚库存？
    //--创建新的表进行库存锁定记录。to do

    @Query(value = "update ware_info set product_number = product_number + ?1 where skuId =?2 and shopId =?3 and id =?4", nativeQuery = true)
    @Modifying
    @Transactional
    Integer releaseWare(Integer lockCount, Long skuId, Integer shopId, Integer wareId);
}
